//////////////////////////////////////////////////////////////////////////
// $Id: ObjectPanel.cpp,v 1.5 2009/01/09 15:41:54 sunjun Exp $
//////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "ObjectPanel.h"
#include "Resource.h"

//////////////////////////////////////////////////////////////////////////

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

//////////////////////////////////////////////////////////////////////////
// ObjectPanel
//////////////////////////////////////////////////////////////////////////

BEGIN_MESSAGE_MAP(ObjectPanel, CWnd)	
	ON_WM_CREATE()
	ON_WM_SIZE()
	ON_COMMAND(ID_OBJECT_NEW_GROUP, &ObjectPanel::OnObjectNewGroup)
	ON_COMMAND(ID_OBJECT_NEW_OBJECT, &ObjectPanel::OnObjectNewObject)
	ON_COMMAND(ID_OBJECT_COPY, &ObjectPanel::OnObjectCopy)
	ON_COMMAND(ID_OBJECT_CUT, &ObjectPanel::OnObjectCut)
	ON_COMMAND(ID_OBJECT_PASTE, &ObjectPanel::OnObjectPaste)
	ON_COMMAND(ID_OBJECT_DELETE, &ObjectPanel::OnObjectDelete)
END_MESSAGE_MAP()

ObjectPanel::ObjectPanel()
{
	
}

ObjectPanel::~ObjectPanel()
{

}

int ObjectPanel::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CWnd::OnCreate(lpCreateStruct) == -1) return -1;

	// create the object combo:
	{
		if (!mComboBox.Create(WS_CHILD|WS_VISIBLE|CBS_DROPDOWNLIST|WS_BORDER|CBS_SORT|WS_CLIPSIBLINGS|WS_CLIPCHILDREN, 
			CRect(0,0,0,0), this, ID_NODE_PANE_COMBO)) return -1;	
		mComboBox.SetFont(CFont::FromHandle((HFONT) GetStockObject(DEFAULT_GUI_FONT)));
		mComboBox.AddString(_T("Environment"));
		mComboBox.AddString(_T("Terrain"));
		mComboBox.SetCurSel(0);
	}

	// create the tool bar
	{
		mToolBar.Create(this, AFX_DEFAULT_TOOLBAR_STYLE, IDT_OBJECT_PANEL_BAR);
		mToolBar.SetFont(CFont::FromHandle((HFONT) GetStockObject(DEFAULT_GUI_FONT)));
		mToolBar.LoadToolBar(IDT_NODE_PANE_BAR, 0, 0, TRUE);
		mToolBar.CleanUpLockedImages();
		mToolBar.LoadBitmap(IDB_OBJECT_PANEL_BAR, 0, 0, TRUE);
		mToolBar.SetPaneStyle(mToolBar.GetPaneStyle()|CBRS_TOOLTIPS|CBRS_FLYBY);
		mToolBar.SetPaneStyle(mToolBar.GetPaneStyle()&~(CBRS_GRIPPER|CBRS_SIZE_DYNAMIC|CBRS_BORDER_TOP|CBRS_BORDER_BOTTOM|CBRS_BORDER_LEFT|CBRS_BORDER_RIGHT));
		mToolBar.SetOwner(this);	
		mToolBar.SetRouteCommandsViaFrame(FALSE);
	}

	// create the splitter window
	{
		CRect rect_client;
		GetClientRect(rect_client);
		int toolbar_cy = mToolBar.CalcFixedLayout(FALSE, TRUE).cy;
		int splitter_cy = rect_client.bottom-(rect_client.top+toolbar_cy);
		if( !mSplitterWnd.CreateStatic(this, 2, 1) ) return -1;
		CCreateContext* pContext = (CCreateContext*)lpCreateStruct->lpCreateParams;
		if( !mSplitterWnd.CreateView(0, 0, RUNTIME_CLASS(ObjectShell), CSize(0, splitter_cy/2), pContext) ) return -1;
		if( !mSplitterWnd.CreateView(1, 0, RUNTIME_CLASS(ObjectView), CSize(0, splitter_cy/2), pContext) ) return -1;
	}

	return 0;
}

void ObjectPanel::OnSize(UINT nType, int cx, int cy)
{
	CWnd::OnSize(nType, cx, cy);

	// adjust the tool bar and the node tree position
	CRect rect_client; GetClientRect(rect_client);
	CRect rect_combo; mComboBox.GetWindowRect(&rect_combo);
	int cyCmb = rect_combo.Size().cy;
	int cyTlb = mToolBar.CalcFixedLayout(FALSE, TRUE).cy;
	mComboBox.SetWindowPos(NULL, rect_client.left, rect_client.top, rect_client.Width(), 200, SWP_NOACTIVATE | SWP_NOZORDER);
	mToolBar.SetWindowPos(NULL, rect_client.left, rect_client.top+cyCmb, rect_client.Width(), cyTlb, SWP_NOACTIVATE|SWP_NOZORDER);
	mSplitterWnd.SetWindowPos(NULL, rect_client.left, rect_client.top+cyCmb+cyTlb, rect_client.Width(), rect_client.bottom-(rect_client.top+cyCmb+cyTlb), SWP_NOACTIVATE|SWP_NOZORDER);
}

void ObjectPanel::OnObjectNewGroup()
{
/*	HTREEITEM hRoot = mTreeCtrl.GetRootItem();
	HTREEITEM hGroup = mTreeCtrl.InsertItem(_T("group"), 1, 1, hRoot);
	mTreeCtrl.SetItemData(hGroup, 0);
	mTreeCtrl.RedrawWindow();
	*/
}

void ObjectPanel::OnObjectNewObject()
{
/*	CString class_name;
	mObjectCombo.GetLBText(mObjectCombo.GetCurSel(),class_name);

	GNodePtr node_ptr;
	if( class_name == "Environment" )
	{
	//	GEnvironmentPtr environment_ptr = dynamic_cast<GEnvironment*>(GEnvironment::creator());
	//	CHECK(environment_ptr);
	//	node_ptr = static_ptr_cast<GNode>(environment_ptr);
	}
	else if( class_name == "Terrain" )
	{
	//	GTerrainPtr terrain_ptr = dynamic_cast<GTerrain*>(GTerrain::creator());
	//	CHECK(terrain_ptr);
	//	node_ptr = static_ptr_cast<GNode>(terrain_ptr);
	}

	// add the new node to the node table
//	if( node_ptr != NULL ) gNodes.push_back(node_ptr);
*/
}

void ObjectPanel::OnObjectCopy()
{

}

void ObjectPanel::OnObjectCut()
{

}

void ObjectPanel::OnObjectPaste()
{

}

void ObjectPanel::OnObjectDelete()
{
/*	HTREEITEM hItem = mTreeCtrl.GetSelectedItem();
	if( hItem && hItem != mTreeCtrl.GetRootItem() ) 
	{
		mTreeCtrl.DeleteItem( hItem );
		mTreeCtrl.RedrawWindow();
	}
	*/
}

//////////////////////////////////////////////////////////////////////////
// ObjectShell
//////////////////////////////////////////////////////////////////////////

IMPLEMENT_DYNCREATE(ObjectShell, CWnd)
BEGIN_MESSAGE_MAP(ObjectShell, CWnd)
	ON_WM_CREATE()
	ON_WM_SIZE()
	ON_WM_CONTEXTMENU()
	ON_NOTIFY(TVN_SELCHANGED, ID_OBJECT_PANEL_TREE, &ObjectShell::OnSelchangedTree)	
END_MESSAGE_MAP()

ObjectShell::ObjectShell()
{

}

ObjectShell::~ObjectShell()
{

}

int ObjectShell::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CWnd::OnCreate(lpCreateStruct) == -1) return -1;	

	// create the object tree	
	if (!mTreeCtrl.Create(WS_CHILD|WS_VISIBLE|TVS_HASLINES|TVS_LINESATROOT|
		TVS_HASBUTTONS|TVS_EDITLABELS|TVS_SHOWSELALWAYS|WS_CLIPSIBLINGS|WS_CLIPCHILDREN, 
		CRect(0,0,0,0), this, ID_OBJECT_PANEL_TREE)) return -1;
	mTreeCtrl.SetFont(CFont::FromHandle((HFONT) GetStockObject(DEFAULT_GUI_FONT)));

	// set the image list for the object tree
	CBitmap bmp; if (!bmp.LoadBitmap(IDB_OBJECT_PANEL_TREE)) return -1;
	BITMAP bmpObj; bmp.GetBitmap(&bmpObj);	
	mImageList.Create(16, bmpObj.bmHeight, ILC_MASK|ILC_COLOR24, 0, 0);
	mImageList.Add(&bmp, RGB(255, 0, 0));
	mTreeCtrl.SetImageList(&mImageList, TVSIL_NORMAL);

	// set the tree root
	HTREEITEM hRoot = mTreeCtrl.InsertItem(_T("Object Groups"), 0, 0);
	mTreeCtrl.SetItemState(hRoot, TVIS_BOLD, TVIS_BOLD);	

	return 0;
}

void ObjectShell::OnSize(UINT nType, int cx, int cy)
{
	CWnd::OnSize(nType, cx, cy);
	mTreeCtrl.SetWindowPos (NULL, -1, -1, cx, cy, SWP_NOMOVE|SWP_NOACTIVATE|SWP_NOZORDER);
}

void ObjectShell::OnContextMenu(CWnd* pWnd, CPoint point)
{
	if (pWnd != &mTreeCtrl)
	{
		CWnd::OnContextMenu(pWnd, point);
		return;
	}

	if (point != CPoint(-1, -1))
	{
		// Select clicked item:
		CPoint ptTree = point;
		mTreeCtrl.ScreenToClient(&ptTree);

		UINT flags = 0;
		HTREEITEM hTreeItem = mTreeCtrl.HitTest(ptTree, &flags);
		if (hTreeItem == NULL) return;		
		mTreeCtrl.SelectItem(hTreeItem);
		mTreeCtrl.SetFocus();

		CMenu menu;
		menu.LoadMenu(IDM_NODE_PANE_TREE);
		CMenu* pSumMenu = menu.GetSubMenu(0);
		if (AfxGetMainWnd()->IsKindOf(RUNTIME_CLASS(CFrameWndEx)))
		{
			CMFCPopupMenu* pPopupMenu = new CMFCPopupMenu;
			if (!pPopupMenu->Create(this, point.x, point.y, (HMENU)pSumMenu->m_hMenu, FALSE, TRUE))
				return;
			((CFrameWndEx*)AfxGetMainWnd())->OnShowPopupMenu(pPopupMenu);
			UpdateDialogControls(this, FALSE);
		}
	}
}

void ObjectShell::OnSelchangedTree(NMHDR* pNMHDR, LRESULT* pResult) 
{
	HTREEITEM hItem = mTreeCtrl.GetSelectedItem();
	if( hItem )
	{
		// entity_t* pEntity = reinterpret_cast<entity_t*>(mTreeCtrl.GetItemData(hItem));
		CString S1 = mTreeCtrl.GetItemText(hItem);
		//	AfxMessageBox("You have selected "+S1);	
	}
	*pResult = 0;
}

/////////////////////////////////////////////////////////////////////////////
// ObjectView
//////////////////////////////////////////////////////////////////////////

IMPLEMENT_DYNCREATE(ObjectView, CWnd)
BEGIN_MESSAGE_MAP(ObjectView, CWnd)
	ON_WM_CREATE()
	ON_WM_SIZE()
END_MESSAGE_MAP()

ObjectView::ObjectView()
{

}

ObjectView::~ObjectView()
{

}

int ObjectView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CWnd::OnCreate(lpCreateStruct) == -1) return -1;	

	// create object panel tabs control:	
	if(!mTabCtrl.Create(CMFCTabCtrl::STYLE_3D, CRect(0,0,0,0), this, 1, CMFCTabCtrl::LOCATION_TOP)) return -1;
	mTabCtrl.SetImageList(IDB_OBJECT_PANEL_TAB_ICON, 16, RGB (192, 192, 192));

	if (!mPropertyCtrl.Create(WS_VISIBLE | WS_CHILD, CRect(0,0,0,0), &mTabCtrl, ID_PROPERTY_PANEL)) return -1;      // fail to create
	mPropertyCtrl.SetFont(CFont::FromHandle((HFONT)GetStockObject(DEFAULT_GUI_FONT)));
	mPropertyCtrl.EnableHeaderCtrl(FALSE);
	mPropertyCtrl.EnableDescriptionArea();
	mPropertyCtrl.SetVSDotNetLook();
	mPropertyCtrl.MarkModifiedProperties();

	if (!mPreviewCtrl.Create( AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS,
		::LoadCursor(NULL, IDC_ARROW), reinterpret_cast<HBRUSH>(COLOR_BTNSHADOW), NULL), 
		NULL, AFX_WS_DEFAULT_VIEW, CRect(0, 0, 0, 0), &mTabCtrl, AFX_IDW_PANE_FIRST, NULL)) return -1;	

	// add the panel to the tabs
	CString strPanelName;
	strPanelName.LoadString(IDS_PROPERTY_PANEL);
	mTabCtrl.AddTab(&mPropertyCtrl, strPanelName, 0 );
	strPanelName.LoadString(IDS_PREVIEW_PANEL);
	mTabCtrl.AddTab(&mPreviewCtrl, strPanelName, 1 );

	return 0;
}

void ObjectView::OnSize(UINT nType, int cx, int cy)
{
	CWnd::OnSize(nType, cx, cy);
	mTabCtrl.SetWindowPos (NULL, -1, -1, cx, cy, SWP_NOMOVE|SWP_NOACTIVATE|SWP_NOZORDER);
}

//////////////////////////////////////////////////////////////////////////
// ObjectPreview
//////////////////////////////////////////////////////////////////////////

IMPLEMENT_DYNCREATE(ObjectPreview, CWnd)
BEGIN_MESSAGE_MAP(ObjectPreview, CWnd)
	ON_WM_CREATE()	
	ON_WM_PAINT()	
	ON_WM_DESTROY()
END_MESSAGE_MAP()

ObjectPreview::ObjectPreview()
{

}

ObjectPreview::~ObjectPreview()
{

}

int ObjectPreview::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CWnd::OnCreate(lpCreateStruct) == -1) return -1;

	// get the windows dc
	mDC = ::GetDC( m_hWnd ); 	

	return 0;
}

void ObjectPreview::OnDestroy()
{
	// release the dc
	if( mDC ) {::ReleaseDC( m_hWnd, mDC );mDC = NULL;}

	CWnd::OnDestroy();
}

void ObjectPreview::OnPaint()
{
	CPaintDC dc(this);
}

//////////////////////////////////////////////////////////////////////////
